package com.guaoran.study.rocketmq;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

import java.io.InputStream;
import java.util.concurrent.Semaphore;

/**
 * @Desc： todo xxx
 * @Auth gucheng
 * @Date 2020/11/6 下午4:47.
 */
public class Producer {

    private static int count = 100;

    public static void main(String[] args) throws Exception {
        // 设置生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
        // 指定nameServer的地址
        producer.setNamesrvAddr("39.97.172.91:9876");
        // 启动实例
        producer.start();

        for (int i = 0; i < count; i++) {
            Thread.sleep(3000);
            Message message = new Message("TopicTest",
                    "test_tag",
                    ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));

            producer.send(message, new SendCallback() {
                public void onSuccess(SendResult sendResult) {
                    System.out.println(String.format("message [%s] send success!", new String(message.getBody())));
                }

                public void onException(Throwable throwable) {
                    throwable.printStackTrace();
                }
            });

        }
        InputStream in = System.in;
        //关闭生产者，释放资源
        producer.shutdown();
    }
}